package com.gxp.web.controller;

import com.gxp.common.annotation.Audit;
import com.gxp.common.constant.UserConstants;
import com.gxp.common.core.controller.BaseController;
import com.gxp.common.core.domain.AjaxResult;
import com.gxp.common.core.domain.entity.SysUser;
import com.gxp.common.core.page.TableDataInfo;
import com.gxp.common.enums.BusinessAction;
import com.gxp.common.enums.BusinessTitle;
import com.gxp.common.utils.DateUtils;
import com.gxp.system.domain.ZkInstrument;
import com.gxp.system.domain.ZknjProject;
import com.gxp.system.domain.ZknjService;
import com.gxp.system.service.ISysUserService;
import com.gxp.system.service.IZkInstrumentService;
import com.gxp.system.service.IZknjProjectService;
import com.gxp.system.service.IZknjServiceService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目Controller
 * 
 * @author zh
 * @date 2024-07-09
 */
@Controller
@RequestMapping("/system/project")
public class ZknjProjectController extends BaseController
{
    private String prefix = "system/project";

    @Autowired
    private IZknjProjectService zknjProjectService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IZknjServiceService zknjServiceService;
    @Autowired
    private IZkInstrumentService instrumentService;


    @RequiresPermissions("system:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("system:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZknjProject zknjProject)
    {
        startPage();
        List<ZknjProject> list = zknjProjectService.selectZknjProjectList(zknjProject);
        return getDataTable(list);
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysUser> verifiers = userService.selectUserByRole(UserConstants.ROLE_VERIFIER);
        List<SysUser> requesters = userService.selectUserByRole(UserConstants.ROLE_REQUESTER);
        //仪器
        List<ZkInstrument> instruments = instrumentService.selectZkInstrumentList(new ZkInstrument());
        mmap.addAttribute("verifiers", verifiers);
        mmap.addAttribute("requesters", requesters);
        mmap.addAttribute("instruments", instruments);
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("system:project:add")
    @Audit(businessTitle = BusinessTitle.PROJECT, businessAction = BusinessAction.CREATED, idToObjParamNames = {"managers", "requesters", "instIds", "services"}, description = "新增项目")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZknjProject zknjProject, String managers, String requesters, String services)
    {
        zknjProject.setCreateBy(getLoginName());
        int row = zknjProjectService.insertZknjProject(zknjProject, managers, requesters, services);
        return toAjax(row, zknjProject.getProId());
    }

    /**
     * 修改项目
     */
    @RequiresPermissions("system:project:edit")
    @GetMapping("/edit/{proId}")
    public String edit(@PathVariable("proId") Long proId, ModelMap mmap)
    {
        ZknjProject zknjProject = zknjProjectService.selectZknjProjectByProId(proId);
        mmap.put("zknjProject", zknjProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("system:project:edit")
    @Audit(businessTitle = BusinessTitle.PROJECT, businessAction = BusinessAction.UPDATED, description = "修改项目")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZknjProject zknjProject)
    {
        zknjProject.setUpdateBy(getLoginName());
        zknjProject.setIsLock(zknjProject.getProStatus());
        zknjProject.setProStatus(2l);
        zknjProject.setApproveType("Update Project");
        return toAjax(zknjProjectService.updateZknjProject(zknjProject), zknjProject.getProId());
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("system:project:remove")
    @Audit(businessTitle = BusinessTitle.PROJECT, businessAction = BusinessAction.DELETED, description = "删除项目")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zknjProjectService.deleteZknjProjectByProIds(ids));
    }

    /**
     * 选择服务
     * @return
     */
    @RequiresPermissions("system:project:view")
    @GetMapping("selServices")
    public String selServices(){
        return prefix + "/selectServices";
    }

    /**
     * 选择服务
     * @return
     */
    @RequiresPermissions("system:project:view")
    @PostMapping("/sevList")
    @ResponseBody
    public TableDataInfo sevList()
    {
        startPage();
        List<ZknjService> listOn = zknjServiceService.selectZknjServices(new ZknjService());
        return getDataTable(listOn);
    }

    @PostMapping("/showServices")
    @ResponseBody
    public AjaxResult showServices(@Param("instIds")String instIds)
    {
        if (instIds == null || instIds.isEmpty()){
            return AjaxResult.success();
        }
        String[] ids = instIds.split(",");
        List<ZknjService> services = zknjServiceService.selectByInstIds(ids);
        return AjaxResult.success(services);
    }

    /**
     * 项目审核
     */
    @RequiresPermissions("system:project:approve")
    @GetMapping("/approve")
    public String approve(ZknjProject project, ModelMap mmap){
        ZknjProject zknjProject = zknjProjectService.selectZknjProjectByProId(project.getProId());
        //获取managers, requesters, instruments, services
        mmap.addAttribute("zknjProject", zknjProject);
        mmap.addAttribute("managers", zknjProjectService.getManagersByProId(project.getProId()));
        mmap.addAttribute("requesters", zknjProjectService.getRequestersByProId(project.getProId()));
        mmap.addAttribute("instruments", zknjProjectService.getInstrumentsByProId(project.getProId()));
        mmap.addAttribute("services", zknjProjectService.getServicesByProId(project.getProId()));
        return prefix + "/approve";
    }

    /**
     * 项目审核
     * @param zknjProject
     * @return
     */
    @Audit(businessTitle = BusinessTitle.PROJECT, businessAction = BusinessAction.APPROVED, description = "项目审核")
    @RequiresPermissions("system:project:approve")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(ZknjProject zknjProject){
        zknjProject.setApproveBy(getLoginName());
        zknjProject.setApproveTime(DateUtils.getNowDate());
        return toAjax(zknjProjectService.approve(zknjProject), zknjProject.getProId());
    }

    /**
     * 校验方法名
     */
    @PostMapping("/checkProNameUnique")
    @ResponseBody
    public boolean checkProNameUnique(ZknjProject zknjProject)
    {
        return zknjProjectService.checkProNameUnique(zknjProject);
    }
}
